<template>
  <div class="app-container">
    <el-form
      :model="sestuForm"
      ref="sestuForm"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="请选择考试" prop="radio">
        <el-select
          v-model="sestuForm.examinationId"
          placeholder="请选择考试"
          @change="selectExam"
          filterable
        >
          <el-option
            v-for="item in examData"
            :key="item.value"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="请选择场次" prop="sessionId">
        <div class="nav">
          <div
            v-for="(item, index) in grouData"
            :key="index"
            :class="activeSessionIndex == index ? 'contentchange' : 'content'"
            @click="selectExamSession(item.id, index)"
          >
            {{ item.name }}
          </div>
        </div>
      </el-form-item>
    </el-form>

    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="评阅老师id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入评阅老师id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场次id" prop="sessionId">
        <el-input
          v-model="queryParams.sessionId"
          placeholder="请输入场次id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >批量引用</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDeleteSelect"
          >批量删除</el-button
        >
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            >批量导入</el-button
          >
        </el-col>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="examReviewList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        :show-overflow-tooltip="true"
        align="center"
      />
      <el-table-column
        label="姓名"
        align="center"
        :show-overflow-tooltip="true"
        prop="teacherName"
      />
      <el-table-column
        label="性别"
        align="center"
        :show-overflow-tooltip="true"
        prop="sex"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.sex == 1"> 男 </span>
          <span v-else-if="scope.row.sex == 2"> 女 </span>
          <span v-else> 未知 </span>
        </template>
      </el-table-column>
      <el-table-column
        label="职工号"
        align="center"
        :show-overflow-tooltip="true"
        prop="teacherNo"
      />
      <el-table-column
        label="所属学院"
        align="center"
        :show-overflow-tooltip="true"
        prop="dwmc"
      />
      <el-table-column
        label="状态"
        align="center"
        :show-overflow-tooltip="true"
        prop="dqzt"
      />
      <el-table-column
        label="职称"
        align="center"
        :show-overflow-tooltip="true"
        prop="jzglbmc"
      />
      <el-table-column
        label="职位"
        align="center"
        :show-overflow-tooltip="true"
        prop="jszc"
      />
      <el-table-column
        label="操作"
        align="center"
        :show-overflow-tooltip="true"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam:examinee:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:examinee:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改引入评阅教师对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="90%"
      append-to-body
      :before-close="handleClose"
    >
      <Teacher :showAddBtn="false" @selectTeacher="selectTeacher" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listExamReview,
  getExamReview,
  delExamReview,
  addExamReview,
  updateExamReview,
  exportExamReview,
  getTeacherExamList,
} from "@/api/exam/examReview";

import {
  listExam,
  addExam,
  getExam,
  delExame,
  updateExam,
} from "@/api/exam/set";

import Teacher from "../../../views/system/teacher/index.vue";

export default {
  name: "ExamReview",
  components: {
    Teacher,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引入评阅教师表格数据
      examReviewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        sessionId: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      sestuForm: {},
      examData: [],
      // 选中考试场次索引
      activeSessionIndex: 0,
      //场地列表
      grouData: [],
    };
  },
  created() {
    this.getListexam();
  },
  methods: {
    // 获取选择考试列表
    getListexam() {
      this.loading = true;
      listExam().then((res) => {
        this.examData = res.rows;
        this.loading = false;
        this.sestuForm.examinationId = this.examData[0].id;
        console.log(this.examData);
        this.selectExam(this.sestuForm.examinationId);
      });
    },

    //查询考试详细信息，场次
    selectExam(id) {
      getExam(id).then((res) => {
        this.grouData = res.data.sessionList;
        this.sestuForm.sessionId = this.grouData[0].id;
        this.getList();
      });
    },

    /** 查询引入评阅教师列表 */
    getList() {
      this.loading = true;
      getTeacherExamList(this.sestuForm.sessionId).then((response) => {
        this.examReviewList = response.data;
        this.loading = false;
      });
    },

    handleDeleteSelect() {},

    // 选中某行教师
    selectTeacher(teach) {
      addExamReview({
        userId: teach.userId,
        sessionId: this.sestuForm.sessionId,
      }).then((res) => {
        this.$message({
          showClose: true,
          duration: 1000,
          message: "导入成功",
          type: "success",
        });
      });
    },

    // 切换场次
    selectExamSession(id, index) {
      this.sestuForm.sessionId = id;
      this.activeSessionIndex = index;
      this.getList();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "考生导入";
      this.upload.open = true;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        sessionId: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加引入评阅教师";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getExamReview(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改引入评阅教师";
      });
    },

    // 关闭弹出框后重新查询页面信息
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.reset();
          this.getList();
          done();
        })
        .catch((_) => {});
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateExamReview(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExamReview(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除引入评阅教师编号为"' + row.teacherNo + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delExamReview(row.reviewId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有引入评阅教师数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportExamReview(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped lang="scss">
.nav {
  display: flex;
  flex-wrap: wrap;
}
.content,
.contentchange {
  display: flex;
  border-radius: 10px;
  margin-bottom: 10px;
  padding: 8px 15px;
}
.contentchange {
  margin-right: 10px;
  background-color: #409eff;
  color: #f1f1f1;
}
</style>
